﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TheOutfield.UmbExt.UniversalMediaPicker.Interfaces;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using DropNet;
using DropNet.Models;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Dropbox
{
    public class DropboxProvider : AbstractProvider
    {
        private IConfigControl configControl;
        private ICreateControl createControl;

        public DropboxProvider()
        { }

        public DropboxProvider(string config)
            : base(config)
        { }

        public override string Alias
        {
            get { return "Dropbox"; }
        }

        public override IConfigControl ConfigControl
        {
            get
            {
                if (configControl == null)
                    configControl = new DropboxConfigControl(this, Config);

                return configControl;
            }
        }

        public override ICreateControl CreateControl
        {
            get { throw new NotImplementedException(); }
        }

        public override IList<MediaItem> GetMediaItems(string parentId)
        {
            var mediaItems = new List<MediaItem>();

            var service = new DropboxService(Config);
            var metaData = service.GetMetaDataList(parentId == "-1" ? "" : parentId);

            foreach (var md in metaData)
            {
                if(md.Is_Dir)
                    mediaItems.Add(md.ToMediaItem());
            }

            foreach (var md in metaData)
            {
                if (!md.Is_Dir)
                    mediaItems.Add(md.ToMediaItem());
            }

            return mediaItems;
        }

        public override MediaItem GetMediaItemById(string id)
        {
            var service = new DropboxService(Config);
            var metaData = service.GetMetaDataItem(id == "-1" ? "" : id);
            return metaData.ToMediaItem();
        }
    }
}